/*
 Xern Entity Relocation Network
 Copyright (C) 2010-2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERN_MATHCOMMON_HPP
#define XERN_MATHCOMMON_HPP

#include <cmath>
#define XERN_ALIGNED(x)

namespace Xern
{
  // Some constants
  const float X_PI = float(M_PI);
  const float X_PI_2 = float(M_PI_2);
  const float X_1_PI = float(M_1_PI);
  const float X_EPSILON = 0.00001f;
#ifndef SWIG
#ifdef INFINITY
  const float X_INF = float(INFINITY);
#else
  const float X_INF = float(10000000000000000000000000000000.0f) // Overflow
#endif

#ifdef NAN
  const float X_NAN = NAN;
#else
#ifndef _MSVC_VER
  const float X_NAN = 0.0f/0.0f;
#else
  extern float X_NAN;
#endif
#endif
#endif

  // common functions
  inline float abs(float x)
  {
    //int y = ((int&)x) & 0x7FFFFFFFu;
    //return y;
  	return x > 0 ? x : -x;
  }

  template<typename T>
  inline T max(T a, T b)
  {
    return a > b ? a : b;
  }

  template<typename T>
  inline T min(T a, T b)
  {
    return a < b ? a : b;
  }

  // Trigonometric functions
  inline float sine(float x)
  {
    return std::sin(x);
  }

  inline float cosine(float x)
  {
    return std::cos(x);
  }

  inline float tangent(float x)
  {
    return std::tan(x);
  }

  // Square roots
  inline float invsquareroot(float x)
  {
    // Use the quake approximation
    /*float y = x;
    int &ix = *((int*)&y);
    ix = 0x5f3759df - (ix>>1);
    return y*(1.5f - 0.5f * x * y * y);*/
	return 1.0f/sqrt(x);
  }

  inline float squareroot(float x)
  {
    //return invsquareroot(x)*x;
	  return sqrt(x);
  }

  // Logarithm aproximation
  inline float log2 (float val)
  {
    //assert (val > 0.0);

    int &ival = reinterpret_cast<int&> (val);
    const int    log_2 = ((ival >> 23) & 255) - 128;
    ival &= ~(255 << 23);
    ival += 127 << 23;

    return (val + log_2);
  }

  inline float log(float val)
  {
    return log2(val)*float(0.30102999566398119521);
  }

  inline float ln(float val)
  {
    return log2(val)*float(0.693147181);
  }

  // NAN never compares true to itself: x == x -> false
  inline bool isnan(float x)
  {
    return x == x ? false : true;
  }

  inline int CompareFloat(float a, float b)
  {
	  float delta = a-b;
	  return (delta < -X_EPSILON) ? -1 : (delta > X_EPSILON ? 1 : 0);
  }

}; // namespace Xern

#endif //XERN_MATHCOMMON_HPP

